package spindles.gwt.client;

import spindles.gwt.shared.ImportManager;
import spindles.gwt.shared.ImportManagerAsync;
import spindles.gwt.shared.SettingsDTO;
import spindles.gwt.shared.SettingsManager;
import spindles.gwt.shared.SettingsManagerAsync;
import spindles.gwt.shared.SpindleDTO;
import spindles.gwt.shared.SpindlesManager;
import spindles.gwt.shared.SpindlesManagerAsync;
import spindles.gwt.shared.UserManager;
import spindles.gwt.shared.UserManagerAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;

public class ServerGateway {
	
	private static ServerGateway instance = new ServerGateway();
	
	private UserManagerAsync userService = (UserManagerAsync) GWT.create(UserManager.class);
	
	private SpindlesManagerAsync spindlesService = (SpindlesManagerAsync) GWT.create(SpindlesManager.class);
	
	private ImportManagerAsync importService = (ImportManagerAsync)GWT.create(ImportManager.class);
	
	private SettingsManagerAsync settingsService = (SettingsManagerAsync)GWT.create(SettingsManager.class);
	
	
	private ServerGateway(){		
	}
	
	private static void setServiceEntryPoint(ServiceDefTarget service, String servletPath){
		ServiceDefTarget endpoint = service;		
		endpoint.setServiceEntryPoint(GWT.getHostPageBaseURL() + servletPath);
	}
	
	public static String getBaseURL(){
		return GWT.getHostPageBaseURL() + "../";
	}
	
	public static String getPlotURL(){
		return getBaseURL() + "plot";
	}
	
	public static String getExportURL(){
		return getBaseURL() + "export";
	}
	
	public static void getUserDTO(AsyncCallback callback){		
		setServiceEntryPoint((ServiceDefTarget)instance.userService, "../user");		
		instance.userService.getUserDTO(callback);		
	}
	
	public static void getPersons(AsyncCallback callback) {		
		setServiceEntryPoint((ServiceDefTarget)instance.spindlesService, "../spindles");		
		instance.spindlesService.getPersons(callback);		
	}
	
	public static void getSleeps(long personID, AsyncCallback callback) {		
		setServiceEntryPoint((ServiceDefTarget)instance.spindlesService, "../spindles");		
		instance.spindlesService.getSleeps(personID, callback);		
	}
	
	public static void getEpochs(long sleepPartID, AsyncCallback callback){
		setServiceEntryPoint((ServiceDefTarget)instance.spindlesService, "../spindles");		
		instance.spindlesService.getEpochs(sleepPartID, callback);
	}
	
	public static void getRecentAudits(int numberOfAudits, AsyncCallback callback){
		setServiceEntryPoint((ServiceDefTarget)instance.importService, "../import");		
		instance.importService.getRecentAudits(numberOfAudits, callback);
	}
	
	public static void getAudits(AsyncCallback callback){
		setServiceEntryPoint((ServiceDefTarget)instance.importService, "../import");		
		instance.importService.getAudits(callback);
	}
	
	public static void isImporting(AsyncCallback callback){
		setServiceEntryPoint((ServiceDefTarget)instance.importService, "../import");		
		instance.importService.isImporting(callback);
	}
	
	public static void importSessionPartData(String filename, String channel,
			boolean allowDuplicatePerson, AsyncCallback callback){
		setServiceEntryPoint((ServiceDefTarget)instance.importService, "../import");		
		instance.importService.importSessionPartData(filename, channel, 
				allowDuplicatePerson, callback);
	}
	
	public static void getExportURL(String level, long id, boolean soft, AsyncCallback callback){
		setServiceEntryPoint((ServiceDefTarget)instance.spindlesService, "../spindles");		
		instance.spindlesService.getExportURL(level, id, soft, callback);
	}
	
	public static void getExportPath(int type, long personID, boolean soft, AsyncCallback callback){
		setServiceEntryPoint((ServiceDefTarget)instance.spindlesService, "../spindles");		
		instance.spindlesService.getExportPath(type, personID, soft, callback);
	}
	
	public static void getExportURL(long epochID, SpindleDTO dto, int position,
			AsyncCallback callback) {
		setServiceEntryPoint((ServiceDefTarget)instance.spindlesService, "../spindles");		
		instance.spindlesService.getExportURL(epochID, dto, position, callback);
	}
	
	public static void getSettings(AsyncCallback callback){
		setServiceEntryPoint((ServiceDefTarget)instance.settingsService, "../settings");		
		instance.settingsService.getSettings(callback);
	}
	
	public static void saveSettings(SettingsDTO s, AsyncCallback callback){
		setServiceEntryPoint((ServiceDefTarget)instance.settingsService, "../settings");		
		instance.settingsService.saveSettings(s, callback);		
	}
	
	public static void detectSpindleIndications(String groupName, AsyncCallback callback){
		setServiceEntryPoint((ServiceDefTarget)instance.settingsService, "../settings");		
		instance.settingsService.detectSpindleIndications(groupName, callback);
	}

}
